import os
import time
import CBD_base.common.Ocr as ocr

from CBD_base.common.page_operation import ChormeOprate
import chitubox_website_project.data.elements_location as elements_location


class Page_Login(ChormeOprate):
    def __init__(self):
        ChormeOprate.__init__(self)

    def login(self):
        # 访问页面
        url = elements_location.login_page["url"]
        self.driver.get(url)
        self.driver.maximize_window()
        self.driver.implicitly_wait(20)
        # 点击同意使用cookie
        l = elements_location.login_page["agree_cookie"]
        self.click_element(l)
        time.sleep(2)
        # self.driver.find_element("xpath", "//*[@id='agree-term-btn']")
        # 输入邮箱
        locator = elements_location.login_page["邮箱"]
        self.input_text(locator, "yangsiqin@cbd-3d.com")
        # 输入密码
        locator = elements_location.login_page["密码"]
        self.input_text(locator, "00000000")
        # 记住账号
        locator = elements_location.login_page["记住账号"]
        self.click_element(locator)
        time.sleep(2)
        # 点击登录
        locator = elements_location.login_page["登录"]
        self.click_element(locator)
        self.driver.close()

    def register(self):
        url = elements_location.register_page["url"]
        self.driver.get(url)
        self.driver.maximize_window()
        self.driver.implicitly_wait(20)
        # 点击同意使用cookie
        l = elements_location.login_page["agree_cookie"]
        self.click_element(l)
        time.sleep(2)

        # 获取验证码并保存
        el = self.find_element(elements_location.register_page["verify_code"])
        # random = randint(0, 1000)
        # 保存图片
        el.screenshot("verify_code.png")
        time.sleep(2)
        cwd = os.getcwd()
        filepath = os.path.join(cwd, "verify_code.png")
        text = ocr.image_ocr(filepath)
        if os.path.exists(filepath):  # 检查文件是否存在
            os.remove(filepath)  # 删除文件


if __name__ == '__main__':
    Page_Login().register()
